package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.UserNoticeBean;
import mamikeji.liaowa.widget.slidelistview.SliderView;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class UserNoticeAdapter extends BaseAdapter {
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;
    List<UserNoticeBean> itemNoticeList = new ArrayList<>();

    SliderView slideView;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onDeleteClick(int id);
    }

    public UserNoticeAdapter(Context context, List<UserNoticeBean> itemNoticeList) {
        this.itemNoticeList = itemNoticeList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemNoticeList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemNoticeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        slideView = (SliderView) convertView;
        if (slideView == null) {
            convertView = inflater.inflate(R.layout.item_user_notice, null);

            viewHolder.textView_user_notice_title = (TextView) convertView.findViewById(R.id.textView_user_notice_title);
            viewHolder.textView_user_notice_content = (TextView) convertView.findViewById(R.id.textView_user_notice_content);
            viewHolder.textView_user_notice_time = (TextView) convertView.findViewById(R.id.textView_user_notice_time);
            viewHolder.imageView_user_notice = (ImageView) convertView.findViewById(R.id.imageView_user_notice);
            viewHolder.tv_unread_msg = (TextView) convertView.findViewById(R.id.tv_unread_msg);
            slideView = new SliderView(context);
            slideView.setContentView(convertView);
            slideView.setTag(viewHolder);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) slideView.getTag();
        }

        slideView.shrink();
        slideView.textView_delete.setText(R.string.user_notice_delete);

        UserNoticeBean itemNotice = (UserNoticeBean) getItem(position);

        viewHolder.textView_user_notice_title.setText(itemNotice.getTitle());
        viewHolder.textView_user_notice_content.setText(itemNotice.getContent());
        viewHolder.imageView_user_notice.setImageResource(itemNotice.getIcon());
        if (!TextUtils.isEmpty(itemNotice.getTime())) {
            viewHolder.textView_user_notice_time.setVisibility(View.VISIBLE);
            viewHolder.textView_user_notice_time.setText(itemNotice.getTime());
        } else {
            viewHolder.textView_user_notice_time.setVisibility(View.GONE);
        }
        int unreadMsg = itemNotice.getUnreadMsg();
        if (unreadMsg > 0) {
            viewHolder.tv_unread_msg.setVisibility(View.VISIBLE);
            viewHolder.tv_unread_msg.setText(unreadMsg + "");
        } else {
            viewHolder.tv_unread_msg.setVisibility(View.GONE);
        }

        slideView.setOnItemClickListener(new SliderView.OnItemClickListener() {
            @Override
            public void onDeleteClick() {
                mOnItemClickListener.onDeleteClick(position);
            }
        });
        return slideView;
    }

    public class ViewHolder {
        TextView textView_user_notice_title;
        TextView textView_user_notice_content;
        TextView textView_user_notice_time;
        ImageView imageView_user_notice;
        TextView tv_unread_msg;
    }

}
